package com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.third_parties.common.model.ThirdProperty;
import com.yonyou.brigade.third_parties.common.service.IThirdPropertyService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.ICtripBCReauthorizationPersionService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.util.DeptAuth;
import com.yonyou.brigade.third_parties.ctripBC.dao.CtripBCUserMapper;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCConfig;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCUser;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCPublicService;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCUserService;
import com.yonyou.brigade.third_parties.ctripBC.util.CtripBCUserConvertUtil;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.opensaml.xml.signature.J;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class CtripBCReauthorizationPersionServiceImpl implements ICtripBCReauthorizationPersionService {

    Logger logger = LoggerFactory.getLogger(CtripBCReauthorizationPersionServiceImpl.class);

    @Autowired
    @Qualifier("userCenterServiceImpl")
    private IUserCenterService iUserCenterService;

    @Autowired
    private IBriChannelService briChannelService;

    @Autowired
    private IThirdPropertyService thirdPropertyService;

    @Autowired
    private ICtripBCUserService ctripBCUserService;

    @Autowired
    private CtripBCUserMapper ctripBCUserMapper;

    @Autowired
    private ICtripBCPublicService ctripBCPublicService;


    /*
    按照人员设置二次授权人
     */
    @Override
    public JSONObject updateUserInfos(String org , String tenantid , List<String> userids , String email) {
        JSONObject result=new JSONObject();
        BriChannel briChannel = briChannelService.getEnableChannelByPersonTenantOrgAndChannel(tenantid,org, ChannelEnum.CTRIPBC.getName());
        if(briChannel==null){
            result.put("code","1");
            result.put("infromation" , "操作失败");
            return result;
        }
        CtripBCConfig config = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
        if(tenantid!=null&&!CollectionUtils.isEmpty(userids)&&email!=null) {
//            List<String> deptpks=new ArrayList<>();
//            for(int i=0;i<deptAuths.size();i++) {
//                String curdeptId=deptAuths.get(i).getDeptpk();
//                deptpks.add(curdeptId);
//            }
            Wrapper<CtripBCUser> ctripBCUserWrapper = new EntityWrapper<>();
            ctripBCUserWrapper.where("tenant={0}", tenantid);
            //已同步到携程大客户的人员
            List<CtripBCUser> ctripBCUsers = ctripBCUserMapper.selectList(ctripBCUserWrapper);
            Set<String> ctripBCUserids = new HashSet<>();
            for(CtripBCUser ctripBCUser:ctripBCUsers){
                ctripBCUserids.add(ctripBCUser.getUserId());
            }
            List<ThirdProperty> thirdProperties = thirdPropertyService.getThirdPropertyByTenantOrgChannel(tenantid, org, ChannelEnum.CTRIPBC.getName());
//            if(!CollectionUtils.isEmpty(userids)){
//                Iterator it = userids.iterator();
//                while(it.hasNext()){
//                    if(ctripBCUserids.contains(it.next())){
//                        it.remove();
//                    }
//                }
//            }
//
//            //同步未同步过的用户
//            if(!CollectionUtils.isEmpty(userids)){
//                List<UcUserDto> ucUserDtos = iUserCenterService.getCompleteUcUserDtoByUserid(userids,tenantid);
//                List<CtripBCUser> syncCtripBCUsers = CtripBCUserConvertUtil.getCtripBCUser(ucUserDtos , config.getCorporationId() , tenantid , org , thirdProperties);
//                ctripBCPublicService.syncCtripBCUsers(syncCtripBCUsers, config);
//                ctripBCUserService.insertBatch(syncCtripBCUsers);
//            }


            result= this.updateAuthUser(userids , org , tenantid  , email);

        //更新完成修改库中数据在报账中实现。
//        for(int i=0;i<deptAuths.size();i++) {
//            String curdeptId=deptAuths.get(i).getDeptpk();
//            Map params=new HashMap();
//            params.put("orgpk",org);
//            params.put("deptpk",curdeptId);
//            DeptAuth deptAuth=ctripUserMapper.selectSingleDeptAuth(params);
//            if(deptAuth!=null)
//            {
//                ctripUserMapper.updateDeptAuthInfo(deptAuths.get(i));
//            }
//            else
//            {
//                ctripUserMapper.insertDeptAuthInfo(deptAuths.get(i));
//            }
        }else {
            result.put("code","1");
            result.put("infromation","缺少参数");
        }

//        result.put("code","0");
//        result.put("information","操作成功");
        return result;
    }

    @Override
    public JSONObject updateAuthUser(List<String> userids, String orgpk,  String tenantid , String email){
        JSONObject result = new JSONObject();
        try {
            BriChannel briChannel = briChannelService.getEnableChannelByPersonTenantOrgAndChannel(tenantid,orgpk, ChannelEnum.CTRIPBC.getName());
            CtripBCConfig config = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
            List<ThirdProperty> thirdProperties = thirdPropertyService.getThirdPropertyByTenantOrgChannel(tenantid, orgpk, ChannelEnum.CTRIPBC.getName());
            if(briChannel!=null) {
//                if (BriChannel.NEEDSYNCUSER == briChannel.getNeedSyncUser()) {
                    if (userids != null && userids.size() > 0) {
                        //将ctripBCUsers中的人员和authUserid一起查询是否同步到携程，将未同步过的人员进行同步
                        List<UcUserDto> ucUserDtos = iUserCenterService.getCompleteUcUserDtoByUserid(userids, tenantid);
                        List<CtripBCUser> syncCtripBCUsers = CtripBCUserConvertUtil.getCtripBCUser(ucUserDtos, config.getCorporationId(), tenantid, orgpk, thirdProperties, briChannel);
                        //调用携程批量同步人员接口
                        ctripBCPublicService.syncCtripBCUsers(syncCtripBCUsers, config, email, briChannel);
                    }
//                } else {
//                    logger.info("该租户已设置不同步人员");
//                    result.put("code","1");
//                    result.put("infromation" , "操作失败");
//                    return result;
//                }
            }
            result.put("code","0");
            result.put("infromation" , "操作成功");
            return result;
        } catch (Exception e) {
            result.put("code","1");
            result.put("infromation" , "操作失败");
            e.printStackTrace();
            return result;
        }
    }


}
